Ext.define('appmod.controller.knowledge.FileStartup', {
    extend: 'Ext.app.Controller',
    
   	stores:[
		'knowledge.category.FileTree',
		'knowledge.content.FileBigView'
   	],   

    views: [
		'knowledge.FileTreeDashboard',
		'knowledge.content.BigView',
		'knowledge.content.UploadFileForm'
    ],
	
    refs: [
		{ref: 'knowledgeTree', selector: 'knowledge-treedashboard knowledge-tree-grid'},
		{ref:'mainContentPanel' ,selector:'knowledge-treedashboard tabpanel' },
		{ref:'mainDashboardPanel' ,selector:'knowledge-treedashboard'},
		{ref:'viewFormatCombo' ,selector:'knowledge-treedashboard combobox'}
    ],	

    init: function() {
		var me = this;

    	me.control({

			'knowledge-treedashboard knowledge-tree-grid' : {
				select: {
					fn:me.selectItem,
					scope:me
				}
			},			
			'knowledge-treedashboard button[action=delete]': {
				click:{
					fn:me.showDeleteItemDialog,
					scope:me
				}
			},
			'knowledge-treedashboard menuitem[action=addAttachment]': {
				click:{
					fn:me.showUploadAttachmentForm,
					scope:me
				}
			},
			'content-list menuitem[action=deleteProducts]': {
				click:{
					fn:me.showDelProductsDialog,
					scope:me
				}
			}											
    	});
    },

	
	/**
	 * load main compont in this controller to framework container
	 */
	loadInitComp : function() {
		var me = this;
		// --- defined store ---
		var treeCategory = me.getKnowledgeCategoryFileTreeStore();
		
		var stores = {
			'category' : treeCategory
		};

		var mainPanel = Ext.create('appmod.view.knowledge.TreeDashboard' , {
			region : 'center',
			stores : stores,
			border:false,
			padding:'0 0 0 0'
		});
		return mainPanel;		
	},
	

	// --- use this app lication
	onLaunch:function(app) {
		var me = this;



	},
	
	selectItem:function(rowModel, record, index, eOpts) {
		var me = this;
		
		var picStore = me.getKnowledgeContentBigViewStore();
	
		var isLeaf = record.get('leaf');
		
		// --- reflash panel ---
		if (isLeaf) {
			
			
			var tabPanel =  me.getMainContentPanel();

			var viewFormatCombo = me.getViewFormatCombo();
			var selValue = viewFormatCombo.getValue();
			if (!selValue) {
				Ext.MessageBox.show({
	        		title: "提示",
	        		msg: '请选择显示的格式',
	        		buttons: Ext.MessageBox.OK,
	        		icon: Ext.MessageBox.WARNING
	       		});
				return;				
			}
			
			var displayTitle = viewFormatCombo.getRawValue() ;
			
			// --- defind and load object ---
			var proxy = picStore.getProxy();
			proxy.extraParams = {
				categoryId:record.getId(),
				viewformat:selValue
			}
			picStore.load();
			
			
			// --- get mark ---
			var selActivePanel = tabPanel.down('knowledge-content-bigview[category='+selValue+']');

			if (!selActivePanel) {
				// --- add new content panel ---
				var bigView = {
					title:displayTitle,
					xtype:'knowledge.content.bigview',
					store:picStore,
					category:selValue
				};
				
				tabPanel.add(bigView);				
			} else {
				
				// --- load current ---
				selActivePanel.bindStore(picStore);
			}
			
		}

	},
	
	showDeleteItemDialog: function(){
	
		var me = this;
		var mainContentPanel = me.getMainContentPanel();
		var currentPanel = mainContentPanel.down('knowledge-content-bigview');
		var selMod = currentPanel.getView().getSelectionModel();
		
		var recs = selMod.getSelection();
		
		
		// --- select resource --
		var removeRecs = "";
		for (var i = 0; i < recs.length; i++) {
			if (removeRecs.length != 0) {
				removeRecs += ",";
			}
			
			removeRecs += recs[i].get('fileName');
			console.info( removeRecs );
		}
		Ext.MessageBox.confirm('确认', '确定要删除信息及文件: "' + removeRecs + '"?', me.confirmDelContent, me);
	
	},
	
	
	confirmDelContent:function() {
		
		
	},
	
	
	showUploadAttachmentForm:function() {
		var me = this;
		// --- get the parent id ---
		var knowledgeTree = me.getKnowledgeTree();
		var selMod = knowledgeTree.getSelectionModel();
		var recs = selMod.getSelection();
		
		if (recs.length == 0) {
			Ext.MessageBox.show({
        		title: "提示",
        		msg: '请先选择知识分类',
        		buttons: Ext.MessageBox.OK,
        		icon: Ext.MessageBox.WARNING
       		});
			return ;
		} else {
			var isLeaf = recs[0].get('leaf');
			if ( !isLeaf ) {
				Ext.MessageBox.show({
	        		title: "提示",
	        		msg: '不能选择父级节点 "'+recs[0].get('text')+'".',
	        		buttons: Ext.MessageBox.OK,
	        		icon: Ext.MessageBox.WARNING
	       		});
				return ;				
			}			
		}		



		// --- define category store ---
		var dsCategoryTree = me.getKnowledgeCategoryTreeStore();
	
		var fieldStores = {
			'dsParentCategory' : dsCategoryTree
		};
		
		// --- build new form ---
		var win = null;
		if (!win) {
			win = new Ext.create('widget.window', {
				title : '信息分类',
				closable : true,
				width : 300,
				height : 160,
				modal:true,
				layout: 'border',
				bodyStytle : 'padding : 5px',
				items:[{
					padding : 5,
					fieldStores:fieldStores,
					defaults: {
        				labelWidth:65
    				},
					xtype : 'knowledge.content.uploadfileform',
					region:'center'
				}],
				dockedItems: [{
				    xtype: 'toolbar',
				    dock: 'bottom',
				    ui: 'footer',
				    items: [
						'->',
				        {
            				text: '保 存',
							action:'uploadFile'
        				}
				    ]
				}]	
			});
		}
		
		// --- find form ---
		var form = win.down("knowledge-content-uploadfileform");
		
		if (recs && recs.length == 1) {
			// --- reset parent id ---
			var ffPCategoryId = form.down('hiddenfield[name=categoryId]');
			ffPCategoryId.setValue(recs[0].getId());
		}
		
		var saveBtn = win.down('button[action="uploadFile"]');
		saveBtn.on("click",me.uploadFile , me , {
			window : win
		});

		if ( !win.isVisible() ) {
			win.show();
		}		
	},
	
	uploadFile:function(btn , event , eOpts) {
		var me = this;

		var upWin = eOpts.window;
		
		var formPanel = upWin.down('knowledge-content-uploadfileform');
		var win = upWin;
		
		var form = formPanel.getForm();
		form.submit({
			url:'../../data/resource/knowledge/content/submit_uploadfile.json',
			waitMsg: '文件上传中...',
			success:function(form , action) {
				 // --- reflash store ---
				 win.close();
				 
				 Ext.Msg.alert('成功', action.result.msg);
			},
			failure: function(form, action){
				switch (action.failureType) {
					case Ext.form.action.Action.CLIENT_INVALID:
						Ext.Msg.alert('失败', '表单里包含错误的信息');
						break;
					case Ext.form.action.Action.CONNECT_FAILURE:
						Ext.Msg.alert('失败', '服务器连接失败,请联系管理人员');
						break;
					case Ext.form.action.Action.SERVER_INVALID:
						Ext.Msg.alert('失败', action.result.msg);
				}
			}
		});			
	}
	
	
	
		
});